package org.tong.rabbitmq;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;


//@Service
public class RabbitmqService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void setCallBack() {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if (ack) {
                    System.out.println("消息成功消费");
                } else {
                    System.out.println("消息消费失败:" + cause);
                }
            }
        });
    }

    public void sendMsg(String msg) {

        rabbitTemplate.convertAndSend(RabbitmqConf.msgQueueName, msg);
    }
}
